/**
 * 2017，thor.com，all rights reserved。
 * 
 * Project：		palmmall-core
 * Filename：	SalesRptDaoImpl.java
 * Created：		2017年4月27日
 * Creator：		wolv
 */
package com.thor.palmmall.dao.rpt.shop;

import java.util.List;

import javax.persistence.Query;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.thor.commons.exception.ThorServiceException;
import com.thor.commons.util.Assert;
import com.thor.dao.BasicDao;
import com.thor.palmmall.api.rpt.Report;
import com.thor.palmmall.api.rpt.shop.RentalReportParam;

/**
 *
 */
@Transactional(rollbackFor = Exception.class, propagation = Propagation.NOT_SUPPORTED)
public class ShopDaoImpl extends BasicDao implements ShopDao {

	@Override
	@SuppressWarnings("unchecked")
	public Report calcRentalReport(RentalReportParam param) throws ThorServiceException {
		Assert.assertArgumentNotNull(param, "param");

		param.validate();

		Query query = RentalReportHqlBuilder.getInstance().build(param);
		List<Object[]> rawReports = query.getResultList();

		return Report.readFrom(rawReports);
	}

	@Override
	@SuppressWarnings("unchecked")
	public Report getFloorArea(String storeId) throws ThorServiceException {
		Assert.assertArgumentNotNull(storeId, "storeId");

		String hql = "select floor, sum(area) from " + PShop.class.getName() + " where storeId = :storeId group by floor order by floor asc";
		Query query = em.createQuery(hql);
		query.setParameter("storeId", storeId);
		List<Object[]> rawReports = query.getResultList();

		return Report.readFrom(rawReports);
	}

	@Override
	@SuppressWarnings("unchecked")
	public Report getFloorShopCount(String storeId) throws ThorServiceException {
		Assert.assertArgumentNotNull(storeId, "storeId");

		String hql = "select floor, count(shopId) from " + PShop.class.getName() + " where storeId = :storeId group by floor order by floor asc";
		Query query = em.createQuery(hql);
		query.setParameter("storeId", storeId);
		List<Object[]> rawReports = query.getResultList();

		return Report.readFrom(rawReports);
	}
}
